package servlet.admin;

import entity.Blog;
import service.BlogService;
import service.Impl.BlogServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.Date;

@WebServlet(urlPatterns = "/admin/BlogEdit")
public class BlogEditServlet extends HttpServlet {
    private BlogService blogService = new BlogServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Integer id = Integer.parseInt(req.getParameter("id"));
        String actionTime = req.getParameter("actionTime");
        String title = req.getParameter("title");
        String address = req.getParameter("address");
        String peoples = req.getParameter("peoples");
        String event = req.getParameter("event");

        Blog blog = blogService.findById(id);
        blog.setActionTime(Date.valueOf(actionTime));
        blog.setTitle(title);
        blog.setAddress(address);
        blog.setPeoples(peoples);
        blog.setEvent(event);

        blogService.update(blog);
        resp.sendRedirect(req.getContextPath() + "/admin/BlogListService");
    }
}